package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.common.response.sys.NewsCategoryResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.NewsCategoryRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.NewsCategoryMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.NewsCategory;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.INewsCategoryService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 新闻分类业务层处理
 *
 * @author mars
 * @date 2024-03-03
 */
@Slf4j
@Service
@AllArgsConstructor
public class NewsCategoryServiceImpl implements INewsCategoryService {

    private final NewsCategoryMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NewsCategory add(NewsCategoryRequest request) {
        NewsCategory entity = NewsCategory.builder().build();
        BeanUtils.copyProperties(request, entity);
        if (request.getParentId() == null) {
            entity.setParentId(0L);
        }
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(NewsCategoryRequest request) {
        NewsCategory entity = NewsCategory.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public NewsCategory getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<NewsCategory> pageList(NewsCategoryRequest request) {
        Page<NewsCategory> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<NewsCategory> query = this.buildWrapper(request);
        IPage<NewsCategory> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<NewsCategory> list(NewsCategoryRequest request) {
        LambdaQueryWrapper<NewsCategory> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public List<NewsCategoryResponse> tree(NewsCategoryRequest queryDto) {
        List<NewsCategory> list = baseMapper.selectByCondition(queryDto);
        List<NewsCategoryResponse> voList = new ArrayList<>();
        for (NewsCategory sysMenu : list) {
            NewsCategoryResponse sysMenuVo = new NewsCategoryResponse();
            if (sysMenu.getParentId().equals(0L)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    @Override
    public List<NewsCategoryResponse> getChildren(List<NewsCategory> list, Long id) {
        List<NewsCategoryResponse> voList = new ArrayList<>();
        for (NewsCategory sysMenu : list) {
            NewsCategoryResponse sysMenuVo = new NewsCategoryResponse();
            if (sysMenu.getParentId() != null && sysMenu.getParentId().equals(id)) {
                BeanUtils.copyProperties(sysMenu, sysMenuVo);
                sysMenuVo.setChildren(getChildren(list, sysMenu.getId()));
                voList.add(sysMenuVo);
            }
        }
        return voList;
    }

    private LambdaQueryWrapper<NewsCategory> buildWrapper(NewsCategoryRequest param) {
        LambdaQueryWrapper<NewsCategory> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getCategoryName())) {
            query.like(NewsCategory::getCategoryName, param.getCategoryName());
        }
        if (param.getParentId() != null) {
            query.eq(NewsCategory::getParentId, param.getParentId());
        }
        return query;
    }

}
